package com.dbq.fast.business.me.ui;

import android.app.Dialog;
import android.content.Context;
import android.graphics.drawable.ColorDrawable;
import android.support.annotation.NonNull;
import android.text.SpannableString;
import android.text.Spanned;
import android.text.style.DynamicDrawableSpan;
import android.text.style.ImageSpan;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.view.Window;
import android.widget.LinearLayout;
import android.widget.TextView;

import com.dbq.fast.App;
import com.dbq.fast.R;

/**
 * 简单分享模块对对话框
 * Created by Administrator on 2018/1/3 0003.
 */

public class ShareDialog extends Dialog{
    public static final String NUMBER_FIRST = "1";
    public static final String NUMBER_SECOND = "2";
    public static final String NUMBER_THIRDE = "3";


    public ShareDialog(@NonNull Context context) {
        super(context);
        requestWindowFeature(Window.FEATURE_NO_TITLE);
        getWindow().setBackgroundDrawable(new ColorDrawable(0x00ffffff));
    }

    public static class Builder {
        private String message;
        private View contentView;
        private String positiveButtonText;
        private String negativeButtonText;
        private String singleButtonText;
        private View.OnClickListener positiveButtonClickListener;
        private View.OnClickListener negativeButtonClickListener;
        private View.OnClickListener singleButtonClickListener;

        private View layout;
        private ShareDialog dialog;

        public Builder(Context context) {
            //这里传入自定义的style，直接影响此Dialog的显示效果。style具体实现见style.xml
            dialog = new ShareDialog(context);
            LayoutInflater inflater = (LayoutInflater) context.getSystemService(Context.LAYOUT_INFLATER_SERVICE);
            layout = inflater.inflate(R.layout.share_activity_dialog_layout, null);
            dialog.addContentView(layout, new ViewGroup.LayoutParams(ViewGroup.LayoutParams.MATCH_PARENT, ViewGroup.LayoutParams.WRAP_CONTENT));
        }

        public Builder setMessage(String message) {
            this.message = message;
            return this;
        }

        public Builder setContentView(View v) {
            this.contentView = v;
            return this;
        }

        public Builder setPositiveButton(String positiveButtonText, View.OnClickListener listener) {
            this.positiveButtonText = positiveButtonText;
            this.positiveButtonClickListener = listener;
            return this;
        }

        public Builder setNegativeButton(String negativeButtonText, View.OnClickListener listener) {
            this.negativeButtonText = negativeButtonText;
            this.negativeButtonClickListener = listener;
            return this;
        }

        public Builder setSingleButton(String singleButtonText, View.OnClickListener listener) {
            this.singleButtonText = singleButtonText;
            this.singleButtonClickListener = listener;
            return this;
        }

        /**
         * 创建单按钮对话框
         * @return
         */
        public ShareDialog createSingleButtonDialog() {
            layout.findViewById(R.id.ok_btn).setOnClickListener(singleButtonClickListener);
            //如果传入的按钮文字为空，则使用默认的“返回”
            if (singleButtonText != null) {
                ((TextView) layout.findViewById(R.id.ok)).setText(singleButtonText);
            } else {
                ((TextView) layout.findViewById(R.id.ok)).setText("好的");
            }
            create();
            return dialog;
        }

        /**
         * 单按钮对话框和双按钮对话框的公共部分在这里设置
         */
        private void create() {
            if (message != null) {      //设置提示内容,构造ImageSpan来处理
                SpannableString spannableString = new SpannableString("你已成功获得 天套餐时长奖励");
                ImageSpan imageSpan = null;
                switch (message){
                   case NUMBER_FIRST:
                        imageSpan = new ImageSpan(App.getContext(), R.drawable.num_first, DynamicDrawableSpan.ALIGN_BASELINE);
                        break;
                    case NUMBER_SECOND:
                        imageSpan = new ImageSpan(App.getContext(), R.drawable.num_second, DynamicDrawableSpan.ALIGN_BASELINE);
                        break;
                    case NUMBER_THIRDE:
                        imageSpan = new ImageSpan(App.getContext(), R.drawable.num_third, DynamicDrawableSpan.ALIGN_BASELINE);
                        break;
                }
                spannableString.setSpan(imageSpan, 5, 6, Spanned.SPAN_INCLUSIVE_EXCLUSIVE);
                ((TextView) layout.findViewById(R.id.message)).setText(spannableString);
            } else if (contentView != null) {       //如果使用Builder的setContentview()方法传入了布局，则使用传入的布局
                ((LinearLayout) layout.findViewById(R.id.content)).removeAllViews();
                ((LinearLayout) layout.findViewById(R.id.content))
                        .addView(contentView, new ViewGroup.LayoutParams(ViewGroup.LayoutParams.MATCH_PARENT, ViewGroup.LayoutParams.MATCH_PARENT));
            }
            dialog.setContentView(layout);
            dialog.setCancelable(false);     //用户可以点击手机Back键取消对话框显示
            dialog.setCanceledOnTouchOutside(false);//用户不能通过点击对话框之外的地方取消对话框显示
        }
    }
}
